// pages/address/list/list.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1,//1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '选择地址', //导航栏 中间的标题
      bg: '#fff',//导航背景
      pagepath:''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    //choice==1  选择地址
    // order==1  全部订单修改地址
    if (options.order == 1) {
      _this.setData({
        choice: options.choice,
        order: options.order,
        orderid: options.orderid,
      })
    } else {
      if (options.choice == 1) {
        _this.setData({
          choice: options.choice,
          id: options.addressid
        })
      } else {
        _this.setData({
          choice: options.choice,
          pagepath:options.pagepath
        })
      }
    }


  },
  onShow() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.getUserAddressList({
      login_token: wx.getStorageSync('login_token'),
    }).then((res) => {
      _this.setData({
        addressList: res.data.data,
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //添加地址
  onAddAddress() {
    if(this.data.order == 1){
      wx.navigateTo({
        url: '/pages/address/modifyNew/modifyNew?order=1&orderid=' + this.data.orderid,
      })
    }else{
      wx.navigateTo({
        url: '/pages/address/modifyNew/modifyNew?pagepath=' + this.data.pagepath,
      })
    }
    
  },
  //修改地址
  onModifyAddress(e) {
    if(this.data.order == 1){
      wx.navigateTo({
        url: '/pages/address/modifyNew/modifyNew?order=1&orderid=' + this.data.orderid + '&id=' + e.currentTarget.dataset.id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/address/modifyNew/modifyNew?id=' + e.currentTarget.dataset.id,
      })
    }
    
  },
  //删除地址
  onDeleteAddress(e) {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.delUserAddress({
      login_token: wx.getStorageSync('login_token'),
      id: _this.data.id
    }).then((res) => {
      _this.setData({
        loadModal: false,
        modalName: null
      })
      _this.onShow()
      wx.showToast({
        title: '删除成功',
        icon: 'none',
        duration: 2000
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false,
        modalName: null
      })
    })

  },
  //选择地址
  onChoiceAddress(e) {
    var _this = this;
    var choice = _this.data.choice;
    if (_this.data.order == 1) {
      _this.setData({
        loadModal: true
      })
      $api.editOrderAddress({
        login_token: wx.getStorageSync('login_token'),
        order_id: _this.data.orderid,
        address_id:e.currentTarget.dataset.id,
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        wx.navigateBack({
          delta: 1,
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else {
      wx.setStorageSync('addressid', e.currentTarget.dataset.id)
      if (choice) {
        wx.navigateBack({
          delta: 1,
        })
      }
    }

  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target,
      id: e.currentTarget.dataset.id
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      id: ''
    })
  },

})